package com.wfm.leetcode.editor.cn;

import com.wfm.common.TreeNode;
import java.util.ArrayList;
import java.util.List;

public class POne14二叉树展开为链表{
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {

    public void flatten(TreeNode root) {
        List<TreeNode> list = new ArrayList<>();
        preorderTraversal(root, list);
        int size = list.size();
        for (int i = 1; i < size; i++) {
            TreeNode prev = list.get(i - 1), curr = list.get(i);
            prev.left = null;
            prev.right = curr;
        }
    }

    public void preorderTraversal(TreeNode root, List<TreeNode> list) {
        if (root != null) {
            list.add(root);
            preorderTraversal(root.left, list);
            preorderTraversal(root.right, list);
        }
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new POne14二叉树展开为链表().new Solution();
    }
}